package com.xiaotao.controller;

import com.xiaotao.dto.TeamPostDTO;
import com.xiaotao.entity.TeamPost;
import com.xiaotao.service.TeamPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/posts")
public class TeamPostController {

    @Autowired
    private TeamPostService teamPostService;

    @GetMapping
    public ResponseEntity<List<TeamPost>> getPosts(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ResponseEntity.ok(teamPostService.getPosts(page, size));
    }

    @GetMapping("/{id}")
    public ResponseEntity<TeamPost> getPostById(@PathVariable Long id) {
        TeamPost post = teamPostService.getPostById(id);
        return post != null ? ResponseEntity.ok(post) : ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<TeamPost> createPost(
            @RequestHeader("X-User-ID") Integer userId,
            @RequestBody TeamPostDTO postDTO) {
        TeamPost post = teamPostService.createPost(userId, postDTO);
        return ResponseEntity.ok(post);
    }

    @PutMapping("/{id}")
    public ResponseEntity<TeamPost> updatePost(
            @PathVariable Long id,
            @RequestHeader("X-User-ID") Integer userId,
            @RequestBody TeamPostDTO postDTO) {
        TeamPost post = teamPostService.updatePost(id, userId, postDTO);
        return post != null ? ResponseEntity.ok(post) : ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deletePost(
            @PathVariable Long id,
            @RequestHeader("X-User-ID") Integer userId) {
        boolean success = teamPostService.deletePost(id, userId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/category/{category}")
    public ResponseEntity<List<TeamPost>> getPostsByCategory(@PathVariable String category) {
        return ResponseEntity.ok(teamPostService.getPostsByCategory(category));
    }

    @GetMapping("/my")
    public ResponseEntity<List<TeamPost>> getUserPosts(@RequestHeader("X-User-ID") Integer userId) {
        return ResponseEntity.ok(teamPostService.getUserPosts(userId));
    }

    @PostMapping("/{id}/like")
    public ResponseEntity<Map<String, Object>> likePost(
            @PathVariable Long id,
            @RequestHeader("X-User-ID") Integer userId) {
        boolean success = teamPostService.likePost(userId, id);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/{id}/unlike")
    public ResponseEntity<Map<String, Object>> unlikePost(
            @PathVariable Long id,
            @RequestHeader("X-User-ID") Integer userId) {
        boolean success = teamPostService.unlikePost(userId, id);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/my/likes")
    public ResponseEntity<List<TeamPost>> getUserLikedPosts(@RequestHeader("X-User-ID") Integer userId) {
        return ResponseEntity.ok(teamPostService.getUserLikedPosts(userId));
    }

    @GetMapping("/recommended")
    public ResponseEntity<List<TeamPost>> getRecommendedPosts(@RequestHeader("X-User-ID") Integer userId) {
        return ResponseEntity.ok(teamPostService.getRecommendedPosts(userId));
    }
}